from flask import g
from toutiao import db
from flask_restful import Resource
class IndexResource(Resource):

    def get(self):
        return {'view':'index'}

from models.news import Channel  # 导入模型类
# 定义字段
from flask_restful import fields, marshal
all_channel_fields = {
    'id': fields.Integer,
    'name': fields.String
}
class AllChannelResource(Resource):

    def get(self):

        # 查询模型类, 获取对象列表
        # channels = Channel.query.filter(Channel.is_visible==True).order_by((Channel.sequence.asc()).all())
        #
        # # 将对象列表转换为字典列表
        # # 返回响应
        # return marshal(channels, all_channel_fields, envelope='channels')

        from cache.channel import AllChannelCache

        return AllChannelCache.get()

from models.news import UserChannel
from toutiao.utils.decorators import loginrequired
from flask import request
from models.news import UserChannel


class UserChannelResource(Resource):

    method_decorators = {
        'put': [loginrequired]
    }

    def get(self):
        # 获取用户的信息
        user_id = g.user_id
        if user_id:
            # 如果是登陆用户, 我们需要查新登陆用户的关注频道
            # channels = UserChannel.query.filter(UserChannel.user_id == user_id,UserChannel.is_deleted == False).order_by(UserChannel.sequence.asc()).all()

            from cache.channel import UserChannelCache
            data_list = UserChannelCache(user_id).get()

            # Userchannel.channel.name 名字
        else:
            # 如果是未登录用户, 我们需要查询 默认的频道
            channels = Channel.query.filter(Channel.is_default == True, Channel.is_visible == True).order_by(Channel.sequence.asc()).all()

            # 必须要有一个推荐
            data_list = [{
                'id': 0,
                'name': '推荐'
            }]

            # 对象列表转换为字典列表
            for item in channels:
                data_list.append({
                    'id': item.id,
                    'name': item.channel.name if user_id else item.name
                })

        # 返回响应
        return {'channels': data_list}

    def put(self):
        """
        只有登陆用户才能修改关注频道

        接受
        验证
        修改
        :return:
        """

        # 接受数据
        user_channels = request.json.get('channels')
        # 验证数据
        UserChannel.query.filter(UserChannel.user_id == g.user_id,
                                 UserChannel.is_deleted == False).update({'is_deleted': True})

        # 对前端传过来的数据进行遍历
        for channel in user_channels:
            # 把删除的标记要改为False
            # 顺序也要修改
            new_data = {
                'is_deleted': False,
                'sequence': channel.get('seq')
            }

            # 对更新的记录进行判断  如果更新失败  说明没有这条记录
            flag = UserChannel.query.filter(UserChannel.channel_id == channel.get('id'),
                                            UserChannel.user_id == g.user_id).update(new_data)

            if flag == 0:
                # 插入数据
                userchannel = UserChannel()
                userchannel.user_id = g.user_id
                userchannel.channel_id = channel.get('id')
                userchannel.is_deleted = False
                userchannel.sequence = channel.get('seq')
                db.session.add(userchannel)

            # 每一次的修给都要提交
            # 保存数据
            db.session.commit()

        # 清除redis的缓存  因为数据库的信息有变动
        from cache.channel import UserChannelCache
        UserChannelCache(g.user_id).clear()

        # 返回响应
        return {'channels': user_channels}, 201























